const _ = require("lodash");
const userInfoModel = require("../../app/models/userInfo.json");
const fileUtil = require("../../app/utils/fileUtil.js");


module.exports = {
    addUserInfo : addUserInfo,
    getUserInfoByUserId : getUserInfoByUserId
}

// add user info to db
function addUserInfo(userInfo, callback){
    let userInfos = getAllUserInfos();
    userInfos[userInfos.length] = userInfo;
    let path = "./app/models/userInfo.json";
    fileUtil.writeFile(path, JSON.stringify(userInfos));
}


function getUserInfoByUserId(userId, callback){
    console.log(userId);
    let result = null;
    _.forEach(userInfoModel, (item) => {
        console.log(item.userId);
        if(_.isEqual(userId, item.userId)){
            result = item;
        }
    });
    callback(null, result);
}

function getAllUserInfos(){
    let userInfos = [];
    let length = 0;
    _.forEach(userInfoModel, (item) => {
        userInfos[length++] = item;
    });
    return userInfos;
}







